package com.weitu.permission.dataAuthority.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.base.entity.Tree;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.dataAuthority.entity.DataAuthority;
import com.weitu.permission.dataAuthority.service.DataAuthorityService;
import com.weitu.permission.role.entity.Role;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/3/21.
 */
@Controller
@RequestMapping(value = "permission/dataAuthority")
public class DataAuthorityController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/dataAuthority";

    private final DataAuthorityService dataAuthorityService;

    @Autowired
    public DataAuthorityController(DataAuthorityService dataAuthorityService){
        this.dataAuthorityService=dataAuthorityService;
    }

    /**
     * 调到数据权限页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 查询选中部门下的角色
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        int count=this.dataAuthorityService.queryCount(paramMap);

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);

        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);
        List<Role> dataList = this.dataAuthorityService.queryRoleList(paramMap);//获取列表数据

        return dataGridPagination.flushDataGridData(dataList);
    }

    /**
     * 弹授予角色部门权限页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/editDataAuthority",method = RequestMethod.GET)
    public ModelAndView editDataAuthority(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("dataAuthority",paramMap);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        return modelAndView;
    }

    /**
     * 编辑角色部门权限
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDataAuthority",method = RequestMethod.POST)
    @SystemLog(module = "数据权限",methods = "授予角色部门权限")
    public AjaxResult editDataAuthorityAjax(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        String deptId = paramMap.get("deptId").toString();
        String roleId = paramMap.get("roleId").toString();
        String dataDeptIdArray = paramMap.get("dataDeptIdArray").toString();

        List<DataAuthority> dataAuthorityList = getDataAuthorityDeptList(deptId,roleId, dataDeptIdArray);
        if (this.dataAuthorityService.updateDataAuthority(deptId,roleId, dataAuthorityList)){
            result.setSuccess(true);
            result.setMessage("角色授予部门权限成功！");
            return result;
        }else {
            result.setMessage("角色授予部门权限失败！");
            return result;
        }
    }

    /**
     * 将角色授予的部门数组，角色的部门ID和角色ID生成list数组
     * @param deptId
     * @param roleId
     * @param dataDeptIdArray
     * @return
     */
    private List<DataAuthority> getDataAuthorityDeptList(String deptId,String roleId, String dataDeptIdArray) {
        List<DataAuthority> dataAuthorityList = new ArrayList<DataAuthority>();

        String[] dataDeptIdArrayString = dataDeptIdArray.split(",");
        for (String dataDeptId : dataDeptIdArrayString) {
            if(dataDeptId !=null&&!dataDeptId.trim().equals("")){
                DataAuthority dataAuthority = new DataAuthority();
                dataAuthority.setDeptId(deptId);
                dataAuthority.setRoleId(roleId);
                dataAuthority.setDataDeptId(dataDeptId);
                dataAuthorityList.add(dataAuthority);
            }
        }
        return dataAuthorityList;
    }
    /**
     * 查角色拥有的部门权限树
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectDataAuthority",method = RequestMethod.POST)
    public List<Tree> selectDataAuthority(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        return this.dataAuthorityService.getDataAuthorityDepartment(paramMap,"0");
    }
}
